package cn.itsource.quartz.job;

import cn.itsource.basic.contants.PayConstants;
import cn.itsource.order.domain.ProductOrder;
import cn.itsource.order.mapper.ProductOrderMapper;
import cn.itsource.pay.domain.PayBill;
import cn.itsource.pay.mapper.PayBillMapper;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

//定义定时任务的业务
@Component
public class MainJob implements Job {
    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private ProductOrderMapper productOrderMapper;
    //修改订单状态
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //1.查询订单  根据什么订单查询？ 查什么订单？
        //1.1 获取JobDetail 里面的参数
        JobDataMap mergedJobDataMap = context.getMergedJobDataMap();
        Map<String,Object> map = (Map<String, Object>) mergedJobDataMap.get("params");
        //获取订单号
        String orderSn = (String)map.get("orderSn");
        //根据订单号 查询 支付单
        PayBill payBill = payBillMapper.loadByOrderSn(orderSn);
        if (payBill.getState()==0){
            //修改订单状态
            payBill.setState(-1);
            payBill.setUpdateTime(new Date());
            //2.判断订单状态
            payBillMapper.update(payBill);
            if (PayConstants.BUSINESSTYPE_PRODUCT.equals(payBill.getBusinessType())){
                ProductOrder productOrder = productOrderMapper.loadById(payBill.getBusinessKey());
                productOrder.setState(-1);
                //3.修改订单
                productOrderMapper.update(productOrder);
            }
        }
    }
}
